<?php
/*
 * 核心数据库初始化类
 * @package    Db
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: b6807c1756ddc213da61381022fc2cdf9a001493 $
 */
defined('IN_IXDCW') or exit('Access Denied');
class CoreDb{
	protected $_db;
	protected $_tableName;
	protected function __construct() {
		global $db;
		$this->_db = &$db;
	}

	/**
	 * child Class get tableName();
	 * @return string
	 */
	protected function tableName(){
		$calledClass = get_called_class();
		$tableNameOriginal = substr($calledClass, 0, strlen($calledClass) - strlen('Db'));
		$tableName = parse_name($tableNameOriginal, 0);
		return $this->_db->getPre() . $tableName;
	}

	protected function count($table, $condition = '', $ttl = 0){
		return $this->_db->count($table, $condition, $ttl);
	}

	protected function fields($isArray = false) {
		static $_fields = array();
		if (!isset($_fields[$this->tableName()])) {
			$file = $this->tableName().'.php';
			if (!PCache::read($file,'fields')) {
				$sql = "SHOW COLUMNS FROM {$this->tableName()}";
				$fields = $this->_db->getCol($sql, 'Field');
				PCache::write($file, $fields, 'fields');
			} else {
				$fields = PCache::read($file, 'fields');
			}
			$_fields[$this->tableName()] = $fields;
		}
		return $isArray ?  $_fields[$this->tableName()] : implode(',', $_fields[$this->tableName()]);
	}
}